package com.cl.blog.web.manager;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cl.blog.annotation.Permissions;
import com.cl.blog.common.Result;
import com.cl.blog.entity.BMenu;
import com.cl.blog.entity.BMenuRole;
import com.cl.blog.entity.BRole;
import com.cl.blog.service.BAdminService;
import com.cl.blog.service.BMenuRoleService;
import com.cl.blog.service.BMenuService;
import com.cl.blog.service.BRoleService;
import com.cl.blog.util.JwtUtil;
import com.cl.blog.vo.RoleMenuVo;
import com.cl.blog.vo.RoleSearchVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author 付
 * @Date 2024-03-21 15:41
 * @Version 1.0
 * @description
 * @since 1.0
 */
@RestController
@RequestMapping("/admin/role")
public class AdminRoleController {
	@Resource
	BRoleService bRoleService;
	@Resource
	BMenuRoleService bMenuRoleService;
	@Resource
	BMenuService bMenuService;
	@Autowired
	BAdminService bAdminService;

	@Autowired
	JwtUtil jwtUtil;


	//	@Login(issuer = Const.GRADE_ADMIN)
	@GetMapping("/list")
	public Result getList() {
		List<BRole> bRoles = bRoleService.list();
		// 获取角色对应的权限
		for (BRole bRole : bRoles) {
			List<BMenuRole> menuRoles = bMenuRoleService.list(new QueryWrapper<BMenuRole>().eq("role_id", bRole.getId()));
			System.out.println("获取的菜单数据"+menuRoles);
			List<Integer> menuIds = menuRoles.stream().map(BMenuRole::getMenuId).collect(Collectors.toList());
			if (menuIds.size() == 0 && bRole.getRole()==0) {
				List<BMenu> bMenus = bMenuService.list();
				List<Integer> ids = bMenus.stream().map(BMenu::getId).collect(Collectors.toList());
				bRole.setMenuIds(ids);
			} else {
				bRole.setMenuIds(menuIds);
			}
		}

		System.out.println("获取的数据"+bRoles);
		return Result.success(bRoles);
	}


	@PostMapping("/update")
//	@Login(issuer = Const.GRADE_ADMIN)
	public Result updateRole(@Validated @RequestBody RoleMenuVo roleMenuVo, HttpServletRequest request) {
		return Result.success(bMenuRoleService.updateByRoleId(roleMenuVo.getMenuIds(), request, roleMenuVo.getRoleId()));
	}

	@GetMapping("/roles")
	public Result rolesList() {
		List<BRole> list = bRoleService.list();
		return Result.success(list);
	}

	@PostMapping("/search")
	public Result searchRole(@RequestBody RoleSearchVo roleSearchVo) {
		List<BRole> bRoles = bRoleService.selectOne(roleSearchVo);
		if (bRoles.size() <= 0) {
			return Result.success();
		}

		System.out.println("获取的搜索表单---->"+roleSearchVo);
		System.out.println("获取的搜索结果---->"+bRoles);

		for (BRole bRole : bRoles) {
			List<BMenuRole> menuRoles = bMenuRoleService.list(new QueryWrapper<BMenuRole>().eq("role_id", bRole.getId()));
			List<Integer> menuIds = menuRoles.stream().map(BMenuRole::getMenuId).collect(Collectors.toList());
			if (menuIds.size() == 0) {
				List<BMenu> bMenus = bMenuService.list();
				List<Integer> ids = bMenus.stream().map(BMenu::getId).collect(Collectors.toList());
				bRole.setMenuIds(ids);
			} else {
				bRole.setMenuIds(menuIds);
			}
		}

		return Result.success(bRoles);
	}

	@PostMapping("/updateRole")
	public Result updateRole(@Validated @RequestBody BRole bRole,HttpServletRequest request){
		System.out.println("修改表单---->"+bRole);
		System.out.println(bRole.getMenuIds());
		bRoleService.updateOrAddRole(bRole,request);
		return Result.success();
	}

	@PostMapping("/delete")
	@Transactional(rollbackFor = Exception.class)
	// @Permissions(role = {1})
	public Result deleteByid(@RequestBody List<Integer> ids){
		Assert.notNull(ids,"抱歉，没有删除的对象");
		if (ids.contains(1)){
			ids.remove(1);
		}
		System.out.println(ids);
		bRoleService.removeBatchByIds(ids);
		bMenuRoleService.remove(new QueryWrapper<BMenuRole>()
				.in("role_id",ids)
		);

		return Result.success();
	}

	/**
	 * 用户授权
	 * @return
	 */
	@GetMapping("/authorization")
	// @Permissions(role = {0})
	public Result authorization(Integer userId,Integer roleId){
		boolean b = bAdminService.authorization(userId,roleId);
		if (!b){
			return Result.error("抱歉，授权失败");
		}
		return Result.success();
	}

	/**
	 * 取消授权
	 * @param userId
	 * @return
	 */
	@GetMapping("/cancel-authorization")
	// @Permissions(role = {0})
	public Result cancelAuthorization(Integer userId){
		boolean b = bAdminService.cancelAuthorization(userId);
		if (!b){
			return Result.error("抱歉，取消授权失败");
		}
		return Result.success();
	}

}
